#include "StdAfx.h"
#include <wx/stdpaths.h>
#include "yasli/Files.h"
#include "yasli/Tokenizer.h"
#include "Serialization.h"
#include "TagPreferences.h"

TagPreferences tagPreferences;

TagPreferences::TagPreferences()
: PreferencesBlock("ctags", true)
, generateTagFiles_(false)
, skipDirectories_(".svn;CVS;Debug;Release")
{
	fileTypes_ = "*.cpp;*.h;*.c;*.cc;*.c++;*.cxx;*.py;*.php";
}

void splitMasks(std::vector<std::string>& masks, const char* stringList)
{
	Tokenizer tokenizer(";", "", "");
	Token tok(stringList, stringList);
	do{
		tok = tokenizer(tok.end());
		masks.push_back(tok.str());
	} while(tok.end() != tok.begin());
}

void TagPreferences::serialize(Archive& ar)
{
	if(!ar.isEdit()){
		ar(fileTypes_, "fileTypes");
		ar(generateTagFiles_, "generateTagFiles");
	}
	ar(skipDirectories_, "skipDirectories");
	if(ar.isInput())
		 splitMasks(skipDirectoriesList_, skipDirectories_.c_str());
}

bool TagPreferences::shallBeScanned(const char* fileName) const
{
	return true;
}


std::string TagPreferences::ctagsExecutable()
{
	if(ctagsExecutable_.empty()){
#ifdef WIN32
		std::string executableDir = Files::extractFilePath(wxStandardPaths::Get().GetExecutablePath().utf8_str());
		ctagsExecutable_ = executableDir + Files::PATH_SEPARATOR + "ctags.exe";
		if(Files::exists(ctagsExecutable_.c_str()))
			return ctagsExecutable_;

		ctagsExecutable_ = executableDir + Files::PATH_SEPARATOR + ".." + Files::PATH_SEPARATOR + "ctags.exe";
		if(Files::exists(ctagsExecutable_.c_str()))
			return ctagsExecutable_;

		ctagsExecutable_.clear();
#else
		ctagsExecutable_ = "/usr/bin/ctags";
#endif
	}
	return ctagsExecutable_;
}

bool TagPreferences::skipDirectory(const char* justName) const
{
	const char* name = Files::extractFileName(justName);
	
	Masks::const_iterator it;
	FOR_EACH(skipDirectoriesList_, it){
		const std::string mask = *it;
		if(mask == name)
			return true;
	}
	return false;
}
